package com.industrial.im.controller;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.im.domain.MessageHistory;
import com.industrial.im.service.IMessageHistoryService;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/im/message")
public class RongYunMessageController {

    @Value("${rongCloud.appSecret}")
    private String appSecret;

    private final IMessageHistoryService messageHistoryService;

    public RongYunMessageController(IMessageHistoryService messageHistoryService) {
        this.messageHistoryService = messageHistoryService;
    }

    /**
     * 全量消息同步
     *
     * @param nonce     随机数
     * @param timestamp 时间戳
     * @param signature 数据签名
     */
    @PostMapping("/messageSync")
    public String messageSync(@RequestParam("nonce") String nonce,
                              @RequestParam("timestamp") String timestamp,
                              @RequestParam("signature") String signature,
                              MessageHistory messageHistory) {
        String[] timestampArgs = timestamp.split(",");
        //加密后生成的签名
        String encodeSignature = SecureUtil.sha1(appSecret + nonce + timestampArgs[0]);
        //对比两个字符串是否相等
        if (!StringUtils.equals(signature, encodeSignature)) {
            return "Error";
        }
        //将聊天记录同步到业务库里面
        messageHistoryService.messageSync(messageHistory);
        return "OK";
    }

    /***
     * 导出单聊的历史消息
     */
    @PostMapping("/exportSingleChatMessage")
    public void exportSingleChatMessage(@RequestBody MessageHistory history, HttpServletResponse response) {
        List<MessageHistory> messageHistories = messageHistoryService.exportSingleChatMessage(history);
        try (ServletOutputStream outputStream = response.getOutputStream();
             ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate("F:\\muchan_hg\\im\\temlapte\\msg_template.xlsx").build();) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(messageHistories, writeSheet);
        } catch (Exception e) {
            throw new RuntimeException("导出错误");
        }
    }

    /***
     * 导出群组的聊天记录
     * @param history
     * @param response
     */
    @PostMapping("/exportGroupChatMessage")
    public void exportGroupChatMessage(@RequestBody MessageHistory history, HttpServletResponse response) {
        List<MessageHistory> messageHistories = messageHistoryService.exportGroupChatMessage(history);
        try (ServletOutputStream outputStream = response.getOutputStream();
             ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate("F:\\muchan_hg\\im\\temlapte\\msg_group_template.xlsx").build();) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(messageHistories, writeSheet);
        } catch (Exception e) {
            throw new RuntimeException("导出错误");
        }
    }
}
